//
//  ShangpinZhanShiViewController.m
//  跨界收
//
//  Created by Allen on 2018/11/4.
//  Copyright © 2018年 gztx. All rights reserved.
//

#import "ShangpinZhanShiViewController.h"
#import "AddChanPinViewController.h"
#import "AddPicItem.h"

@interface ShangpinZhanShiViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *picArray;
@end

@implementation ShangpinZhanShiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商品展示";
    self.picArray = [NSMutableArray array];
    [self setup];
    // Do any additional setup after loading the view from its nib.
}
- (void)setup
{
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2.0-80, 27, 160, 45)];
    addBtn.layer.masksToBounds = YES;
    [addBtn setTitle:@"添加产品" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addBtn.layer.cornerRadius = 3.0;
    WeakSelf;
    [addBtn addClickTargetBlock:^(UIButton *sender) {
        AddChanPinViewController *addVC = [[AddChanPinViewController alloc] initWithNibName:@"AddChanPinViewController" bundle:nil];
        addVC.pictureBlock = ^(NSMutableDictionary * _Nonnull picDic) {
            [weakSelf.picArray addObject:picDic];
            [weakSelf.tableview reloadData];
        };
        [weakSelf.navigationController pushViewController:addVC animated:YES];
    }];
    addBtn.backgroundColor = [UIColor cz_colorWithHex:0xF69E35];
    [footerView addSubview: addBtn];
    self.tableview.tableFooterView = footerView;
}
- (void)initPictureDataForCell:(UITableViewCell *)cell
{
    CGFloat distance = (SCREEN_WIDTH - 60*3) /4.0;
    for (int i = 0; i < self.picArray.count; i++) {
        CGFloat X = distance + (60+distance) * (i%3);
        CGFloat Y = 10 + (10 + 90)* (i/3);
        AddPicItem *item = [[AddPicItem alloc]initWithFrame:CGRectMake(X, Y, 90, 90)];
        item.tag = 100+i;
        NSMutableDictionary *dic = self.picArray[i];
        item.picImageView.image = dic[@"image"];
        item.titleLabel.text = dic[@"title"];
        [cell.contentView addSubview:item];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.picArray.count%3 == 0 ? (self.picArray.count/3 *100):(self.picArray.count/3 *100 + 100);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [self initPictureDataForCell:cell];
    return cell;
}


@end
